module Main where

import Core
import UI

appMain spreadsheet = do 
                         displayMenu spreadsheet             
                         decision <- readNumber                                                
                         
                         if decision == 0 then    
                           do
                             putStrLn "Exiting application"                        
                             exit                                                                          
                         else if decision == 1 then
                           do
                             result <- createNewSpreadsheet
                             return (result)
                         else if decision == 2 then
                           do
                              result <- loadSpreadsheet
                              return (result)  
                         else if isEmpty spreadsheet == False then
                                 if decision == 3 then
                                   do
                                      result <- saveSpreadsheet spreadsheet
                                      return (result) 
                                 else if decision == 4 then
                                   do                              
                                      result <- modifySpreadsheet spreadsheet
                                      return (result) 
                                 else if decision == 5 then
                                   do
                                     result <- insertValueIntoSpreadsheet spreadsheet
                                     return (result)
                                 else if decision == 6 then
                                   do
                                     displaySpreadsheet spreadsheet
                                     return (spreadsheet)   
                                 else
                                   do
                                     putStrLn "Incorrect option. Please try again: "
                                     return (spreadsheet) 
                                                                
                         else
                           do
                             putStrLn "Incorrect option. Please try again: "
                             return (spreadsheet)                                                                                  

appLoop spreadsheet = catch(do
                               tmp <- (appMain spreadsheet)
                               appLoop tmp)
                           (\e -> do 
                                     putStrLn "Error occured in application: "
                                     putStrLn (show e)
                                     appLoop spreadsheet)
                         
main::IO()
main = appLoop [] 